package com.fourthGroup.controller;

import com.fourthGroup.feign.UserClient;
import com.fourthGroup.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/online")
public class OnlineController {

    @Autowired
    private UserClient userClient;

    @Value("${server.port}")
    private String serverPort;

    /**
     * 显示登录用户信息及直播相关信息
     */
    @GetMapping("/live/{uid}")
    public Map<String, Object> showLiveInfo(@PathVariable("uid") Integer uid) {
        // 通过Feign调用用户微服务获取用户信息
        User user = userClient.queryUserById(uid);

        log.info("成功获取用户信息: {}", user);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        if (user != null) {
            result.put("code", 200);
            result.put("message", "获取直播信息成功");
            result.put("user", user);
            result.put("username", user.getUsername());
            result.put("liveStatus", "正在直播中");
            result.put("viewers", (int)(Math.random() * 1000));
            result.put("serverPort", serverPort);
        } else {
            result.put("code", 404);
            result.put("message", "用户不存在");
        }

        return result;
    }
}